package autoteam.ui.dialogs;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import autoteam.League;
import autoteam.tables.WeightEditorTableModel;

public class WeightEditorDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private static WeightEditorDialog dialog = null;
	private static boolean val = true;
	WeightEditorTableModel parserModel = null;

	/**
	 * Set up the dialog. The first argument can be null, but it really should
	 * be a component in the dialog's controlling frame.
	 */
	private static void initialize(JFrame frame, League _doc) {
		dialog = new WeightEditorDialog(frame, _doc);
	}

	/**
	 * Show the initialized dialog. The first argument should be null if you
	 * want the dialog to come up in the center of the screen. Otherwise, the
	 * argument should be the component on top of which the dialog should
	 * appear.
	 */
	public static boolean showDialog(JFrame frame, League _doc) {
		if (dialog == null)
			initialize(frame, _doc);
		else
			dialog.parserModel.setChildren(_doc);
		dialog.setLocationRelativeTo(frame);
		dialog.setVisible(true);

		return val;
	}

	private WeightEditorDialog(JFrame frame, League _doc) {
		super(frame, "WeightEditor", true);

		// buttons
		JButton cancelButton = new JButton("Cancel");
		final JButton okButton = new JButton("OK");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WeightEditorDialog.dialog.setVisible(false);
				WeightEditorDialog.val = false;
			}
		});
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WeightEditorDialog.dialog.setVisible(false);
				WeightEditorDialog.val = true;
				parserModel.transfer();
			}
		});
		getRootPane().setDefaultButton(okButton);

		// the editing table

		parserModel = new WeightEditorTableModel(_doc);
		final JTable table = new JTable(parserModel);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));

		JScrollPane tableScroller = new JScrollPane(table);
		tableScroller.setPreferredSize(new Dimension(250, 80));
		// XXX: Must do the following, too, or else the scroller thinks
		// XXX: it's taller than it is:
		tableScroller.setMinimumSize(new Dimension(250, 80));
		tableScroller.setAlignmentX(LEFT_ALIGNMENT);

		// Create a container so that we can add a title around
		// the scroll pane. Can't add a title directly to the
		// scroll pane because its background would be white.
		// Lay out the label and scroll pane from top to button.
		JPanel tablePane = new JPanel();
		tablePane.setLayout(new BoxLayout(tablePane, BoxLayout.Y_AXIS));
		tablePane.add(Box.createRigidArea(new Dimension(0, 5)));
		tablePane.add(tableScroller);
		tablePane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		// Lay out the buttons from left to right.
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(okButton);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPane.add(cancelButton);

		// Put everything together, using the content pane's BorderLayout.
		Container contentPane = getContentPane();
		contentPane.add(tablePane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		pack();

	}
}
